package Program.RegisterUI;

import Program.LoginUI.LoginUI;
import Program.SqlTool.AdminDAO;
import Program.Tool.Popup;

import javax.swing.*;
import java.awt.event.*;

public class AdminRegisterListener implements ActionListener {
    //用户注册界面
    private JFrame AdminRegisterUI;
    //顶端容器
    private JPanel northPanel;
    //账号输入框
    private JTextField userNameField;
    //密码输入框
    private JPasswordField passwordField;
    //姓名输入框
    private JTextField nameField;
    //性别复选框
    private JComboBox<String> sexBox;
    //住址信息输入框
    private JTextArea addressJTextArea;
    //密钥输入框
    private JTextField keyField;
    //最小化按钮
    private JButton minimizeButton;
    //关闭按钮
    private JButton closeButton;
    //注册按钮
    private JButton registerButton;
    //返回按钮
    private JButton backButton;
    //记录鼠标x轴上的位置
    private int mouseAtX = 0;
    //记录鼠标y轴上的位置
    private int mouseAtY = 0;

    public AdminRegisterListener() {
        super();
    }

    public AdminRegisterListener(JFrame AdminRegisterUI, JPanel northPanel, JTextField userNameField, JPasswordField passwordField,
                                 JTextField nameField, JComboBox<String> sexBox, JTextArea addressJTextArea, JTextField keyField, JButton minimizeButton,
                                 JButton closeButton, JButton registerButton, JButton backButton) {
        this.AdminRegisterUI = AdminRegisterUI;
        this.northPanel = northPanel;
        this.userNameField = userNameField;
        this.passwordField = passwordField;
        this.nameField = nameField;
        this.sexBox = sexBox;
        this.addressJTextArea = addressJTextArea;
        this.keyField = keyField;
        this.minimizeButton = minimizeButton;
        this.closeButton = closeButton;
        this.registerButton = registerButton;
        this.backButton = backButton;
        this.canDraged(this.northPanel);
    }

    //设置窗口移动
    public void canDraged(JPanel p) {
        //创建接收鼠标事件的监听器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //当鼠标按下时获取窗口的坐标位置
            public void mousePressed(MouseEvent e) {
                mouseAtX = e.getPoint().x;
                mouseAtY = e.getPoint().y;
            }
        };
        MouseMotionAdapter motionAdapter = new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                //设置获取拖拽后的位置并窗口的位置
                AdminRegisterUI.setLocation((e.getXOnScreen() - mouseAtX), (e.getYOnScreen() - mouseAtY));
            }
        };
        //给顶部容器添加监听事件
        p.addMouseListener(mouseAdapter);
        p.addMouseMotionListener(motionAdapter);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == registerButton) {
            registerCheck();
        } else if (e.getSource() == closeButton) {
            //触发关闭事件
            System.exit(0);
        } else if (e.getSource() == minimizeButton) {
            //触发最小化事件
            AdminRegisterUI.setExtendedState(JFrame.ICONIFIED);
        } else if (e.getSource() == backButton) {
            //触发返回事件
            SwingUtilities.invokeLater(() -> new LoginUI());
            AdminRegisterUI.dispose();
        }
    }

    //注册检查
    private void registerCheck() {
        //从用户名框里读取用户名
        String inputUserName = userNameField.getText();
        //从密码输入框里读取密码
        char[] tempArray = passwordField.getPassword();
        String inputPassword = new String(tempArray);
        //从姓名输入框里读取姓名
        String inputName = nameField.getText();
        //从性别下拉框里读取性别
        String inputSex = sexBox.getSelectedItem().toString();
        //从住址信息输入框里读取门牌信息
        String inputAddress = addressJTextArea.getText();
        //从密钥输入框里获取密钥
        String inputKey = keyField.getText();
        //提示信息d
        String info = "";
        if (inputUserName.length() * inputPassword.length() * inputName.length() * inputAddress.length() == 0) {
            // 判断所有输入框是否为空
            info = "<html>注册失败,请将所有信息完善</html>";
            Popup.showJDialog(info);
        } else if (!(inputUserName.length() >= 4 && inputUserName.length() <= 17)) {
            //判断用户名格式是否正确，最小长度4，最大长度17
            info = "<html>用户名要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            userNameField.setText("");
        } else if (!(inputPassword.length() >= 4 && inputPassword.length() <= 17)) {
            //判断密码格式是否正确，最小长度4，最大长度17
            info = "<html>密码要求：最小长度为4，最大长度为17</html>";
            Popup.showJDialog(info);
            //清空输入框
            passwordField.setText("");
        } else if (!nameCheck(inputName)) {
            //判断姓名格式是否正确
            info = "<html>请填写正确的中文姓名</html>";
            Popup.showJDialog(info);
        } else if (!AFCheck(inputAddress)) {
            //判断门牌信息格式是否正确
            info = "<html>住址信息格式：XX省/区XX市/县XX镇xx村xx栋xx号</html>";
            Popup.showJDialog(info);
            //清空输入框
            addressJTextArea.setText("");
        } else if(!inputKey.equals("admin684GBK")){
            //密钥：admin684GBK
            //判断密钥是否正确
            info = "<html>请输入正确的密钥</html>";
            Popup.showJDialog(info);
            //清空输入框
            keyField.setText("");
        } else {
            //进行注册操作，数据写入数据库
            boolean flag = AdminDAO.addNewAdmin(inputUserName, inputPassword, inputName, inputSex, inputAddress);
            if (flag) {
                info = "<html>注册成功</html>";
                Popup.showJDialog(info);
                SwingUtilities.invokeLater(() -> new LoginUI());
                AdminRegisterUI.dispose();
            }else {
                info = "<html>注册失败</html>";
                Popup.showJDialog(info);
                //清空输入框
                userNameField.setText("");
                //清空输入框
                passwordField.setText("");
                //清空输入框
                nameField.setText("");
                //清空输入框
                addressJTextArea.setText("");
            }
        }
    }

    //姓名格式检查
    private boolean nameCheck(String inputName) {
        String regex = "^(?:[\\u4e00-\\u9fa5·]{2,16})$";
        boolean flag = inputName.matches(regex);
        return flag;
    }

    //住址信息格式检查
    private boolean AFCheck(String inputPlace) {
        //XX省/区XX市/县XX镇xx村xx栋xx号
        String regex = "^[\\u4E00-\\u9FA5]{1,5}(省|区)[\\u4E00-\\u9FA5]{1,5}(市|县)[\\u4E00-\\u9FA5]{1,5}镇[\\u4E00-\\u9FA5]{1,5}村\\d{1,2}栋\\d{1,2}号$";
        boolean flag = inputPlace.matches(regex);
        return flag;
    }
}
